package org.droidcon.apps.template.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class ProfileProvider extends ContentProvider {

	private static final UriMatcher URI_MATCHER;

	private static final int PROFILES = 100;

	private static final int PROFILES_ID = 101;

	private ProfileDatabase databaseHelper;
	
	
    static{
       URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

       URI_MATCHER.addURI(ProfileContract.AUTHORITY, ProfileContract.CONTENT_PATH, PROFILES);
       URI_MATCHER.addURI(ProfileContract.AUTHORITY, ProfileContract.CONTENT_PATH + "/#", PROFILES_ID);      
    }
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase database = databaseHelper.getWritableDatabase();
		
		database.insert(ProfileContract.TABLE, null, values);
		
		// FIXME: mus return a correct uri!
		return null;
	}

	@Override
	public boolean onCreate() {
			databaseHelper = new ProfileDatabase(getContext(), ProfileContract.DATABASE_NAME, null, ProfileContract.DATABASE_VERSION);		
			
			return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
	    switch (URI_MATCHER.match(uri)) {
	      case PROFILES:
	  		
	  		SQLiteDatabase database = databaseHelper.getReadableDatabase();

	        return database.query(ProfileContract.TABLE, null, null, null, null, null, null);	        
	      case PROFILES_ID:
	    	  throw new IllegalArgumentException("cannot query for single entries yet");

	      default:
	        throw new IllegalArgumentException(
	            "Unbekannte URI " + uri);
	    }	
		


	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
